﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.factory;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Imaginecup.util.resourceMan;
using imaginecup.objectManager;
using imaginecup.information;
using imaginecup.util;
using Imaginecup.util;
using Imaginecup.stage;
using Imaginecup.gameState.malaria.stage;
using Imaginecup.util.input;
using Microsoft.Xna.Framework.Input.Touch;
using Imaginecup.util.vertex2D;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Imaginecup.gameState.malaria.component.entity.panel;
using Imaginecup.gameState.malaria.component.factory;
using Imaginecup.sprite;
using Imaginecup.shareData;

namespace Imaginecup.gameState.malaria
{
    public class DarkRainState : GameState
    {
        private IStageMan m_StageMan;
        private ContentManager m_Content;
        private static DarkRainState m_RainState = null;


        private DarkRainState()
        {
            m_Content = new ContentManager(m_Game.Services);
            m_Content.RootDirectory = "Content";
            //제일 처음 생성되는 시점에서 initState를 호출한다.( 호출하지 않으면 ObjectManager


            //Vertex2DPipeMan pipeMan = Vertex2DPipeMan.GetRenderPipeMan();

            //pipeMan.SetScreenSize(new Vector2(800, 480));
            //pipeMan.InitPipeMan2D();

        }

        public static DarkRainState GetRainState()
        {
            if (m_RainState == null)
            {
                m_RainState = new DarkRainState();
            }
            return m_RainState;
        }

        public void SetStageMan(IStageMan stageMan)
        {
           m_StageMan = stageMan;
        }

        /************************************************************************/
        /* 초기화 되는 값                                                       */
        /************************************************************************/
        protected override void InitState()
        {
            
        }

        /************************************************************************/
        /* ContentMan에 리소스 로드(배경,메뉴 리소스)  공통사항                                */
        /************************************************************************/
        protected override void LoadStateResource()
        {
            ResourceMan.GetResourceMan().SetTextureFrame("Back" , m_Content.Load<Texture2D>("darkRain\\temp_Back1"));

            m_StageMan.LoadStageData();

        }


        /************************************************************************/
        /* 리소스해제                                                                     */
        /************************************************************************/
        protected override void UnloadResource()
        {

#if WINDOWS
            m_Game.Components.Remove(KeyboardInputHandler.GetInputHandler());
#endif
            m_StageMan.Release();
            ResourceMan.GetResourceMan().Release();
            SoundMan.GetSoundMan().ReleaseSound();
            m_Content.Unload();

            SetHorizontalGraphic();
            
        }

        private void SetHorizontalGraphic()
        {
            GraphicsDeviceManager deviceMan = GraphicShareData.GetShareData().GetGraphicDeviceMan();

            deviceMan.SupportedOrientations = DisplayOrientation.Default;

            deviceMan.PreferredBackBufferHeight = 480;
            deviceMan.PreferredBackBufferWidth = 800;
            deviceMan.ApplyChanges();
        }

        public override void AddStateEvent()
        {
            if (m_GameStateMan.GetTopState() != this)
            {
                this.Enabled = false;
            }
            else
                this.Enabled = true;

            base.AddStateEvent();
        }


        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            IMouseInputHandler input = MouseInputHandler.GetInputHandler();

            //if (input.IsOneClick() )
            //    ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_ChoiceMap));

            IStageLoop stg = m_StageMan.GetStage();
            stg.Update(gameTime);

            //            if ( m_StageMan.IsGameOver() )
            //            {

            ////                ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_Malaria));
            //            }

            //if (m_StageMan.IsClear())
            //{
            //    ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_TitleMenu));
            //}

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sb = m_GraphicData.GetSpriteBatch();

            IStageLoop stg = m_StageMan.GetStage();
            stg.Draw(gameTime , sb);
         //   m_BackSprite.Draw(sb, new EntityInfo(0 , 0 , 480 , 800));
        }
    }
}
